package org.helidb.wroom325;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class SearchFrame extends JFrame {

	private JPanel contentPane;
	private JTextField tf1;
	private JTextField tf2;
	public JEditorPane ep1 = new JEditorPane();
	JPanel sp = new JPanel();

	SearchMethod method;

	public Vector<JLabel> lines = new Vector<JLabel>();
	JScrollPane scroll = new JScrollPane();
	private JTextField extTf;
	private JTextField txtUtf;

	public void addLable(String title) {
		JLabel lbl = new JLabel(title);
		lbl.setForeground(Color.RED);
		lbl.setFont(new Font("serif", Font.BOLD, 14));
		lines.add(lbl);
	}

	public void addLable(String title, final String filePath, final int lineNumber) {
		final JLabel lbl = new JLabel(title);
		lbl.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseExited(MouseEvent e) {
				lbl.setForeground(Color.BLACK);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				lbl.setForeground(Color.BLUE);
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				String str = method.readTxtFile(filePath, txtUtf.getText().trim());
				ep1.setText(str);
				ep1.requestFocusInWindow();
				ep1.select(str.indexOf(lineNumber + ""), str.indexOf(lineNumber + 1 + "") - 1);

			}
		});
		lines.add(lbl);
	}

	public void showLables() {
		for (int i = 0; i < lines.size(); i++) {
			sp.add(lines.get(i));
		}
		// this.pack();
		repaint();
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					SearchFrame frame = new SearchFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SearchFrame() {
		setTitle("多关键词检索");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(700, 588);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.4);
		contentPane.add(splitPane, BorderLayout.CENTER);

		JScrollPane scrollPane = new JScrollPane();
		splitPane.setLeftComponent(scrollPane);

		scrollPane.setViewportView(ep1);

		splitPane.setRightComponent(scroll);

		scroll.setViewportView(sp);
		BoxLayout layout = new BoxLayout(sp, BoxLayout.Y_AXIS);
		sp.setLayout(layout);

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(800, 100));
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(null);

		tf1 = new JTextField();
		tf1.setText("");
		tf1.setBounds(95, 10, 425, 21);
		panel.add(tf1);
		tf1.setColumns(10);

		JButton btnFile = new JButton("浏览");
		btnFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// onBtnFileClicked
				JFileChooser fileChooser = new JFileChooser("C:\\");
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = fileChooser.showOpenDialog(fileChooser);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String filePath = fileChooser.getSelectedFile().getAbsolutePath();// 这个就是你选择的文件夹的路径
					tf1.setText(filePath);
				}
			}
		});
		btnFile.setBounds(530, 9, 73, 23);
		panel.add(btnFile);

		JLabel lblh = new JLabel("文件夹目录：");
		lblh.setBounds(10, 13, 99, 18);
		panel.add(lblh);

		JLabel lblk = new JLabel("关键词：");
		lblk.setBounds(31, 41, 78, 15);
		panel.add(lblk);

		tf2 = new JTextField();
		tf2.setText("");
		tf2.setBounds(95, 41, 370, 21);
		panel.add(tf2);
		tf2.setColumns(10);

		JButton btnStart = new JButton("搜索");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// onBtnStartClicked
				if (tf1.getText().trim().length() == 0) {
					JOptionPane.showMessageDialog(null, "选择路径！");
					return;
				}
				if (tf2.getText().trim().length() == 0) {
					JOptionPane.showMessageDialog(null, "填关键词！");
					return;
				}
				String path = tf1.getText().trim();
				String[] keyWords = tf2.getText().trim().split("\\s+");
				String[] exts = extTf.getText().trim().split("\\s+");
				String encoding = txtUtf.getText().trim();
				initMethod();
				initSp();
				method.showMatchResult(path, keyWords, exts, encoding);
				showLables();
			}
		});
		btnStart.setBounds(521, 51, 93, 36);
		panel.add(btnStart);

		extTf = new JTextField();
		extTf.setBounds(95, 69, 173, 21);
		extTf.setText(".java");
		panel.add(extTf);
		extTf.setColumns(10);

		txtUtf = new JTextField();
		txtUtf.setText("utf-8");
		txtUtf.setBounds(279, 69, 93, 21);
		panel.add(txtUtf);
		txtUtf.setColumns(10);
		ep1.setEditable(false);
	}

	void initMethod() {
		method = new SearchMethod(this);
		lines = new Vector<JLabel>();
	}

	void initSp() {
		sp = new JPanel();
		BoxLayout layout = new BoxLayout(sp, BoxLayout.Y_AXIS);
		sp.setLayout(layout);
		scroll.setViewportView(sp);
		this.repaint();
	}
}
